package www.golf.web.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Slider;

import www.golf.web.util.UtilEvent.SetEvent;


public class UtilListbox {
	
	public String getPKListcellValue(Component com,String id){
		if(getListitem(com,id)==null){
			try {
				Messagebox.show("尚未选择操作项目");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		int k=getListitem(com,id).getChildren().size()-1;
		return getListcellValue(com,id,k);
	}
	
	public String getListcellValue(Component com,String id,int k){
		if(getListitem(com,id)==null){
			try {
				Messagebox.show("尚未选择操作项目");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		return ((Listcell)getListitem(com,id).getChildren().get(k)).getLabel();
	}
	public String getListcellAttributeValue(Component com,String id,int k,String Attribute_name){
		if(getListitem(com,id)==null){
			try {
				Messagebox.show("尚未选择操作项目");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		return String.valueOf(((Listcell)getListitem(com,id).getChildren().get(k)).getAttribute(Attribute_name));
	}
	public Listitem getListitem(Component com,String id){
		return ((Listbox)com.getFellow(id)).getSelectedItem();
	}
	/**
	 * 由List建立Listbox資料表
	 * @author angle
	 * @param listId Listbox的ID 或是Listbox
	 * @param list 查詢完畢的List(或是集合物件 ) 或是ListModel
	 * @param itemRender render的方式
	 */
	public void setComponentData(Component component,Object listId,Object list,ListitemRenderer itemRender){
		Listbox listdata=null;
		if(listId instanceof String){
			listdata=(Listbox) component.getFellow((String) listId);
		}else if(listId instanceof Listbox){
			listdata=(Listbox) listId;
		}else{
			System.out.println("setComponentData can't find relation obj");
		}
		if(list instanceof Collection&&listdata!=null){
			ListModelList listModel = new ListModelList((Collection) list);
			listdata.setModel(listModel);
			listdata.setItemRenderer(itemRender);
		}else if (list instanceof ListModel&&listdata!=null){
			listdata.setModel((ListModel) list);
			listdata.setItemRenderer(itemRender);
		}else{
			System.out.println("setComponentData can't struct relation obj");
		}
	}
	/**
	 * ListBox資料控制ListBox資料筆數並調整高度,包含Slider
	 * @param component 底層物件
	 * @param highSlider 無連動Slider可輸入null
	 * @param controller 連動的listbox或是boxid
	 * @param maxhigh 連動最大高度
	 * @param minhigh 連動最小高度
	 * @param maxCount 最大筆數
	 * @param minCount 最小筆數
	 * @param level 間距(最大筆數>最小筆數*level*K(自然數))
	 * @return Listbox(已連動,null代表失敗)
	 */
	public Listbox setSilderDataCounter(final Component component,final Slider highSlider,Object controller,final int maxhigh,final int minhigh,final int maxCount,final int minCount,final int level){
		final Listbox pageSelect =new Listbox();
		Object gpslist =null;
		gpslist=controller;
		if(gpslist!=null){
			pageSelect.setMold("select");
			pageSelect.setRows(1);
			SetEvent setEvent =new SetEvent(){
				public void Content() {
					int selectpaging=(Integer) pageSelect.getSelectedItem().getValue();
					int percent=(100*(selectpaging-minCount)/(maxCount-minCount));
					if(highSlider!=null){
					highSlider.setCurpos(percent);
					}
					((HtmlBasedComponent) component).setHeight(String.valueOf((percent*((maxhigh-minhigh)/100))+minhigh)+"px");
				}
			};
			structPageSelect(pageSelect,gpslist,component, maxCount,minCount,level,setEvent);
			return pageSelect;
		}else{
			return null;
		}
	}
	/**
	 * ListBox資料控制ListBox資料筆數並調整高度,不包含Slider
	 * @author angle
	 * @param obj id或是listbox
	 * @param box 表單id 或是 listbox(表單)
	 * @param component 框體
	 * @param maxCount 最大顯示
	 * @param minCount 最小顯示
	 * @param level 間距
	 * @param setEvent 事件補充 null is default
	 */
	public void structPageSelect(Object obj,final Object box,final Component component,final int maxCount,final int minCount,int level,final SetEvent setEvent){
		Listbox templist=null;
		if(obj instanceof String){
			templist= (Listbox) component.getFellow((String) obj);
		}else if(obj instanceof Listbox){
			templist=(Listbox) obj;
		}
		final Listbox list=templist;
		if(list!=null){
			for(int i=minCount;i<=maxCount;i=i+level){
				Listitem item =new Listitem();
				item.setLabel(String.valueOf(i)+"筆");
				item.setValue(i);
				item.setParent(list);
			}
			list.setSelectedIndex(0);
			list.addEventListener("onSelect", new EventListener() {
				public void onEvent(org.zkoss.zk.ui.event.Event event)
				throws Exception {
					try{
						List listbox=new ArrayList();
						if(box instanceof String){
							String[] temp=((String)box).split(",");
							for(int i=0;i<temp.length;i++){
								listbox.add(((Listbox)component.getFellow((String) temp[i])));
							}
						}else if(box instanceof Listbox){
							listbox.add( box);
						}else if(box instanceof List){
							listbox=(List) box;
						}
						int selectpaging=(Integer) list.getSelectedItem().getValue();
						for(Object listboxObj:listbox){
							((Listbox) listboxObj).setPageSize(selectpaging);
						}
						if(setEvent != null){
							setEvent.Content();
						}
					}catch(Exception e){
						System.out.println("structPageSelect ERROR:"+e.toString());
					}
				}	
			});
		}
	}
}
